/*
 * @Author: intellectual-seeker 3336339701@qq.com
 * @Date: 2025-04-11 11:50:05
 * @LastEditors: intellectual-seeker 3336339701@qq.com
 * @LastEditTime: 2025-08-05 12:26:19
 * @FilePath: /code/lesson25/test2/task.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#include <iostream>
#include <unistd.h>
#include <vector>
#include <string>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <time.h>

typedef void (*work_t)(pid_t);
typedef void (*task_t)(pid_t);

void PrintLog(pid_t pid)
{
    std::cout << "pid: " << pid << " task is PrintLog\n" << std::endl;
}

void ReloadConf(pid_t pid)
{
    std::cout << "pid: " << pid << " task is ReloadConf\n" << std::endl;
} 

void ConnectMysql(pid_t pid)
{
    std::cout << "pid: " << pid << " task is ConnectMysql\n" << std::endl;
}

task_t task[3] = {PrintLog, ReloadConf, ConnectMysql};

int NextTask()
{
    return rand() % 3;
}

void work(pid_t pid)
{
    while(true)
    {
        size_t commandcode;
        int n = read(0, &commandcode, sizeof(commandcode));

        //缓冲区刷新？
        //std::cout << "commandcode: " << commandcode << std::endl;

        if(n > 0)//?
        {
            if(commandcode >= 3) continue;//检测命令码合法性
            task[commandcode](pid);
        }
        else if(n == 0)
            break;

        //fflush(FILE*);

        sleep(1);
    }
}